package com.yzjs.base;





import com.google.gson.Gson;
import com.yzjs.config.WxPayConfig;
import com.yzjs.enumeration.BestPayPlatformEnum;
import com.yzjs.enumeration.DownloadBillRequest;
import com.yzjs.enumeration.OrderQueryRequest;
import com.yzjs.enumeration.SignType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.Objects;
import javax.servlet.http.HttpServletRequest;

public class BestPayServiceImpl implements BestPayService {
    private static final Logger log = LoggerFactory.getLogger(WxPayServiceImpl.class);
    private WxPayConfig wxPayConfig;

    public BestPayServiceImpl() {
    }

    public void setWxPayConfig(WxPayConfig wxPayConfig) {
        this.wxPayConfig = wxPayConfig;
    }



    public PayResponse pay(PayRequest request) {
        Objects.requireNonNull(request, "request params must not be null");
        if (BestPayPlatformEnum.WX == request.getPayTypeEnum().getPlatform()) {
            WxPayServiceImpl wxPayService = new WxPayServiceImpl();
            wxPayService.setWxPayConfig(this.wxPayConfig);
            Gson gson  = new Gson();
            log.info( "-------------"+gson.toJson(this.wxPayConfig));
            return wxPayService.pay(request);
        } else {
            throw new RuntimeException("错误的支付方式");
        }
    }

    public PayResponse syncNotify(HttpServletRequest request) {
        return null;
    }

    public boolean verify(Map<String, String> toBeVerifiedParamMap, SignType signType, String sign) {
        return false;
    }

    public PayResponse asyncNotify(String notifyData) {

            WxPayServiceImpl wxPayService = new WxPayServiceImpl();
            wxPayService.setWxPayConfig(this.wxPayConfig);
            return wxPayService.asyncNotify(notifyData);

    }

    public RefundResponse refund(RefundRequest request) {
        WxPayServiceImpl wxPayService = new WxPayServiceImpl();
        wxPayService.setWxPayConfig(this.wxPayConfig);
        return wxPayService.refund(request);
    }



    public OrderQueryResponse query(OrderQueryRequest request) {
        if (request.getPlatformEnum() == BestPayPlatformEnum.WX) {
            WxPayServiceImpl wxPayService = new WxPayServiceImpl();
            wxPayService.setWxPayConfig(this.wxPayConfig);
            return wxPayService.query(request);
        }else {
            throw new RuntimeException("错误的支付平台");
        }
    }

    public String downloadBill(DownloadBillRequest request) {
        WxPayServiceImpl wxPayService = new WxPayServiceImpl();
        wxPayService.setWxPayConfig(this.wxPayConfig);
        return wxPayService.downloadBill(request);
    }

    public String getQrCodeUrl(String productId) {
        WxPayServiceImpl wxPayService = new WxPayServiceImpl();
        wxPayService.setWxPayConfig(this.wxPayConfig);
        return wxPayService.getQrCodeUrl(productId);
    }
}

